package com.arvato.common.webservice.pos;



import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.namespace.QName;
import javax.xml.soap.Name;

import org.apache.axis.client.Call;
import org.apache.axis.client.Service;
import org.apache.axis.message.PrefixedQName;
import org.apache.axis.message.SOAPHeaderElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Component
public class WebServiceUtils {
	private Logger logger = LoggerFactory.getLogger(this.getClass());

	/**
     * 调用ws接口方法
     * @throws Exception
     *
     * */
    public  String PostInterFace(String ownerId, String method,String xmlInput) throws Exception {
    	long start = System.currentTimeMillis();
        //AES加密的key和iv
        String DecryptKey = PosECConfig.decrypt_key;
        String DecryptIV= PosECConfig.decrypt_iv;
        String privateKey = PosECConfig.private_key;
       
        //接口地址
        String PosUrl = "";
        if("10".equals(ownerId)){
            PosUrl = "http://140.207.169.134/PosService-GLS/WS.asmx";
        }else if("11".equals(ownerId)) {
            PosUrl = "http://140.207.169.134/PosService-GLS/WS.asmx";
        }else if("46".equals(ownerId)) {
            PosUrl = "http://140.207.169.134/PosService-GLS/WS.asmx";
        }else if("41".equals(ownerId)) {
            PosUrl = "http://140.207.169.134/PosService-GLS/WS.asmx";
        }

    	MySoapHeader mySoapHeader = new MySoapHeader();
        String reqxml = "";
        String signTime= new SimpleDateFormat("yyyyMMddHHmmss").format(new Date()) +(int)(Math.random()*900 + 100);
        RSASignature signature = new RSASignature();
        // 生成签名
        String signStr = signature.sign(mySoapHeader.getOwnerId(),
                mySoapHeader.getStoreId(), mySoapHeader.getPosId(),
                mySoapHeader.getDeviceId(), signTime,
                mySoapHeader.getSysCode(),privateKey);
        mySoapHeader.setSign(signStr);
        // 加密请求参数
        String EncodeCBC = EncryptUtil.AesEncrypt(xmlInput, DecryptKey, DecryptIV);
        reqxml = EncodeCBC;
        try {
            Service service = new Service();
            Call call = (Call) service.createCall();
            call.setTimeout(5000);
            String url = PosUrl;
            call.setTargetEndpointAddress(new URL(url));
            /** 添加请求的header */
            Name headerName = new PrefixedQName(new QName("http://www.infortech.com.cn/", "MySoapHeader"));
            SOAPHeaderElement head = new SOAPHeaderElement(headerName);
            head.setActor(null);
            head.addChildElement("OwnerId").addTextNode(mySoapHeader.getOwnerId());
            head.addChildElement("StoreId").addTextNode(mySoapHeader.getStoreId());
            head.addChildElement("DeviceId").addTextNode(mySoapHeader.getDeviceId());
            head.addChildElement("Token").addTextNode(signTime);
            head.addChildElement("SysCode").addTextNode(mySoapHeader.getSysCode());
            head.addChildElement("PosId").addTextNode(mySoapHeader.getPosId());
            head.addChildElement("Sign").addTextNode(signStr);
           // head.addChildElement("AppVersion").addTextNode(mySoapHeader.getAppVersion());
            call.addHeader(head);

            call.setUseSOAPAction(true);
            call.setSOAPActionURI("http://www.infortech.com.cn/CallService");
            call.setOperationName(new QName("http://www.infortech.com.cn/","CallService"));// WSDL里面描述的接口名称

            call.addParameter(new QName("http://www.infortech.com.cn/",
                            "method"), org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);
            call.addParameter(new QName("http://www.infortech.com.cn/",
                            "args"), org.apache.axis.encoding.XMLType.XSD_STRING,
                    javax.xml.rpc.ParameterMode.IN);

            call.setReturnType(org.apache.axis.encoding.XMLType.XSD_STRING);// 设置返回类型


            // 返回数据
            String result = (String) call.invoke(new Object[] {method,reqxml});


            // 解密返回数据
            String decodeCBCResult = EncryptUtil.AesDecrypt(result,DecryptKey,DecryptIV);
        	long end = System.currentTimeMillis();
        	logger.error("请求接口用时:" + (end - start));
        	
//            decodeCBCResult = charSetConvert(decodeCBCResult);
            return decodeCBCResult;
        } catch (Exception e) {
        	e.printStackTrace();
            throw new RuntimeException();
        }
    }
    
    public static void main(String[] args) throws Exception {
    			
    	String text="<EC_MemberInfoQuery><OwnerId>10</OwnerId><StoreId>32310000</StoreId><MemberNo>M7800000128</MemberNo></EC_MemberInfoQuery>";
		System.out.println(new WebServiceUtils().PostInterFace("10", "EC_MemberInfoQuery", text));
	}
}
